package com.diapoc.androidteacher.CircleProgressBar;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;

public class CircleProgressBar extends View implements OnTouchListener,
		OnClickListener {

	public int height = 0;
	public int width = 0;
	public int height_mode;
	public int width_mode;
	public Paint paint;

	public CircleProgressBar(Context context) {
		super(context);
		init();
	}

	public CircleProgressBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public CircleProgressBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		drawCanvas(canvas);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		width = MeasureSpec.getSize(widthMeasureSpec);
		height = MeasureSpec.getSize(heightMeasureSpec);
		width_mode = MeasureSpec.getMode(widthMeasureSpec);
		height_mode = MeasureSpec.getMode(heightMeasureSpec);
		initProgressBarInfo();
	}

	@Override
	public void onClick(View v) {

	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		return false;
	}

	public void init() {
		Log.v("test", "init");
		initProgressBar(true);
	}

	// 下面开始是自定义的界面部分
	private int pointX = 0; // 环形圆心x
	private int pointY = 0; // 环形圆心y
	private int strokeWidth = 10; // 环形进度条边框的宽度
	private int pipeWidth = 80; // 环形宽度
	private int radio = 0; // 环形进度条半径
	private int angel_pipe; // 管道初始化旋转角度
	private int angel_water; // 内部水流旋转角度
	private float[] rate = { (int) Math.max(30, (Math.random() * 1000) % 150),
			(int) Math.max(30, (Math.random() * 1000) % 150),
			(int) Math.max(30, (Math.random() * 1000) % 150) };
	private ArrayList<int[]> rgbs = new ArrayList<int[]>();

	private void initProgressBar(boolean withAnimation) {
		initPaint();
		initColor();
		if (withAnimation) {
			th_pipe.start();
		} else {
			angel_pipe = 360;
			th_water.start();
		}
	}

	private void initPaint() {
		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setStyle(Style.STROKE);
		paint.setStrokeWidth(strokeWidth);
	}

	private void initColor() {
		rgbs.add(getRandomRGB());
		rgbs.add(getRandomRGB());
		rgbs.add(getRandomRGB());
		rgbs.add(getRandomRGB());
	}

	private void initProgressBarInfo() {
		pointX = width / 2;
		pointY = height / 2;
		radio = Math.min(width / 2, height / 2) - pipeWidth / 2;
	}

	private void drawCanvas(Canvas canvas) {
		Paint paintCircle = new Paint();
		paintCircle.setStyle(Style.STROKE);
		paintCircle.setStrokeWidth(pipeWidth - strokeWidth);
		paintCircle.setAntiAlias(true);
		paintCircle.setColor(Color.WHITE);

		Paint paintCircleColor = new Paint();
		paintCircleColor.setStyle(Style.STROKE);
		paintCircleColor.setStrokeWidth(pipeWidth);
		paintCircleColor.setAntiAlias(true);
		RectF oval = new RectF(pointX - radio, pointY - radio, pointX + radio,
				pointY + radio);

		// 累计计算角度
		int totalPipe = 0;
		int totalWater = 0;
		for (int i = 0; i < rate.length; i++) {
			if (totalPipe > 360) {
				break;
			}
			if (angel_pipe < totalPipe) {
				break;
			}
			paintCircleColor.setColor(getColorFromRGB(rgbs.get(i)));
			paintCircle.setColor(getLightColor(rgbs.get(i)));
			canvas.drawArc(oval, (int) (-180 + totalPipe),
					(int) (Math.min(Math.min(rate[i], 360 - totalPipe),
							angel_pipe - totalPipe)), false, paintCircleColor);
			canvas.drawArc(oval, (int) (-180 + totalPipe),
					(int) (Math.min(Math.min(rate[i], 360 - totalPipe),
							angel_pipe - totalPipe)), false, paintCircle);
			totalPipe += rate[i];
		}
		if (totalPipe < 360 && angel_pipe >= totalPipe) {
			paintCircleColor.setColor(getColorFromRGB(rgbs.get(3)));
			canvas.drawArc(oval, -180 + totalPipe, Math.min(360, angel_pipe)
					- totalPipe, false, paintCircleColor);
			paintCircle.setColor(getLightColor(rgbs.get(3)));
			canvas.drawArc(oval, (int) (-180 + totalPipe),
					Math.min(360, angel_pipe) - totalPipe, false, paintCircle);
		}

		for (int i = 0; i < rate.length; i++) {
			if (totalWater > 360) {
				break;
			}
			if (angel_water < totalWater) {
				break;
			}
			paintCircle.setColor(getColorFromRGB(rgbs.get(i)));
			canvas.drawArc(oval, (int) (-180 + totalWater), (int) (Math.min(
					Math.min(rate[i], 360 - totalWater), angel_water
							- totalWater)), false, paintCircle);
			totalWater += rate[i];
		}
		if (totalWater < 360 && angel_water >= totalWater) {
			paintCircle.setColor(getColorFromRGB(rgbs.get(3)));
			canvas.drawArc(oval, (int) (-180 + totalWater),
					Math.min(360, angel_water) - totalWater, false, paintCircle);
		}
	}

	private int getRandomColor() {
		return Color.rgb(getRandomRGB()[0], getRandomRGB()[1],
				getRandomRGB()[2]);
	}

	private int getColorFromRGB(int[] rgb) {
		return Color.rgb(rgb[0], rgb[1], rgb[2]);
	}

	private int[] getRandomRGB() {
		int rgb[] = new int[3];
		rgb[0] = (int) (Math.random() * 1000) % 255;
		rgb[1] = (int) (Math.random() * 1000) % 255;
		rgb[2] = (int) (Math.random() * 1000) % 255;
		Log.v("test", rgb[0] + " " + rgb[1] + " " + rgb[2]);
		return rgb;
	}

	private int getLightColor(int[] rgb) {
		float correctionFactor = 0.8f;
		int[] result = new int[3];
		result[0] = (int) ((255 - rgb[0]) * correctionFactor + rgb[0]);
		result[1] = (int) ((255 - rgb[1]) * correctionFactor + rgb[1]);
		result[2] = (int) ((255 - rgb[2]) * correctionFactor + rgb[2]);
		return Color.rgb(result[0], result[1], result[2]);
	}

	// 生成管道线程
	Thread th_pipe = new Thread() {
		public void run() {
			try {
				sleep(500);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			while (angel_pipe < 360) {
				angel_pipe++;
				postInvalidate();
				try {
					sleep(2);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			th_water.start();
		}
	};

	// 水流线程
	Thread th_water = new Thread() {
		public void run() {
			while (angel_water < 360) {
				angel_water++;
				postInvalidate();
				try {
					sleep(25);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	};
}
